// pages/goods_list/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    tabs: [
      {
        id: 1,
        value: "综合",
        isActive: true
      },
      {
        id: 2,
        value: "销量",
        isActive: false
      },
      {
        id: 3,
        value: "价格",
        isActive: false
      }
    ],
    goodsList: []
  },
  queryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  totalPage: 0,
  handleChange(e) {
    console.log(e.detail);
    const index = e.detail;
    let tabs = JSON.parse(JSON.stringify(this.data.tabs));
    tabs.forEach((v, i) => {
      i === index ? (v.isActive = true) : (v.isActive = false);
    });
    this.setData({
      tabs
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryParams.cid = options.cid;
    this.getGoodsList();
  },
  async getGoodsList() {
    const res = await request({ url: "/goods/search", data: this.queryParams });
    console.log(res);
    this.totalPage = Math.ceil(res.total / this.queryParams.pagesize);

    this.setData({
      goodsList: [...this.data.goodsList, ...res.goods]
    });
    wx.stopPullDownRefresh();
  },
  onReachBottom() {
    if (this.queryParams.pagenum >= this.totalPage) {
      wx.showToast({
        title: "没有下一页了"
      });
    } else {
      this.queryParams.pagenum++;
      this.getGoodsList();
    }
  },
  onPullDownRefresh() {
    this.setData({
      goodsList: []
    });
    this.queryParams.pagenum = 1;
    this.getGoodsList();
  }
});
